home *** CD-ROM | disk | FTP | other *** search
- ' QBMATH1.LIB - Math library for Microsoft QuickBASIC
- ' writen in QuickBASIC 4.5
- ' by David Katelansky
- ' 9802 Forum Park
- ' Apartment 3197
- ' Houston, Tx. 77036
- ' registration fee is $10.00
- '
- ' Include File for QBMATH1.LIB
- ' always put the statement 'INCLUDE:'QBMATH1.BI'
- ' at the top of your programs when using this library
- '
- ' this math library can be run from the integrated environment by starting
- ' QuickBASIC with the following statement: QB/L QBMATH1
- '
- ' this math library can be linked into your programs by answering the
- ' library prompt when linking with QBMATH1.LIB (or QBMATH1+, if you have other
- ' libraries to follow)
- '
- DECLARE FUNCTION gcd& (a&, b&) 'greatest common denominator
- ' syntax: greatest& = gcd&(a&, b&)
- '
- DECLARE FUNCTION primefact$ (z&) 'prime factors of an integer
- ' syntax: answer$ = primefact$(z&)
- ' the answer is a string with the integers separated by ":" each
- ' e.g., answer$ = primefact$(12) will result in answer$ becoming
- ' the string 1: 2: 3: 4: 6:
- '
- DECLARE FUNCTION polyarea& (verts%, coord$) ' area of a polygon
- ' syntax: areaofpoly& = polyarea&(verts%, coord$)
- ' verts% is an integer from 1 to 25 (number of vertices in polygon)
- ' coord$ is a string that contains the polygon coordinates
- ' coord$ has the form "x1,y1:x2,y2,x3,y3:"
- ' for as many points as you want, as long as each point is
- ' of the form x,y:
- ' e.g., areaofpoly& = polyarea&(4,"0,0:10,0:10,10:0,10:")
- ' will find the area of a square - 4 vertices, 10 units per side
- ' vertex coordinates (0,0), (10,0), (10,10), (0,10)
- ' with the resul being areaofpoly& being 100
- '
- TYPE Vectortype
- xcoord AS INTEGER ' x coordinate
- ycoord AS INTEGER ' y coordinate
- zcoord AS INTEGER ' z coordinate
- END TYPE
- TYPE VecInfo
- magnitude AS INTEGER ' magnitude of vector
- xangle AS INTEGER ' angle with x-axis
- yangle AS INTEGER ' angle with y-axis
- zangle AS INTEGER ' angle with z-axis
- inclangl AS INTEGER ' included angle, angle with other vector
- END TYPE
- DECLARE FUNCTION FAN! (x AS ANY) ' called by the following subroutine
- ' gives relationship between two vectors - angles, magnitudes
- DECLARE SUB vectorelate (a AS Vectortype, b AS Vectortype, Ainfo AS VecInfo, Binfo AS VecInfo)
- 'syntax: Call vectorelate(A, B, Ainfo, Binfo)
- ' Before calling this routine you must set the values for A and B
- ' using the above types; vectortype and VecInfo.
- ' You need the following definitions:
- ' DIM A AS vectortype
- ' DIM B AS vectortype
- ' DIM Ainfo AS VecInfo
- ' DIM Binfo AS VecInfo
- ' right after the include statement ('$INCLUDE:'QBMATH.BI') in your
- ' program header (i.e., before any executable statements)
- ' example
- 'DIM A AS vectortype
- 'DIM B AS vectortype
- 'DIM Ainfo AS VecInfo
- 'DIM Binfo AS VecInfo
- 'A.xcoord = 3
- 'A.ycoord = 10
- 'A.zcoord = 0
- 'B.xcoord = 2
- 'B.ycoord = 5
- 'B.zcoord = 0
- 'CALL vectorelate(A, B, Ainfo, Binfo)
- 'PRINT Ainfo.magnitude
- 'PRINT Binfo.magnitude
- 'PRINT Ainfo.xangle, Ainfo.yangle, Ainfo.zangle, Ainfo.inclangl
- 'PRINT Binfo.xangle, Binfo.yangle, Binfo.zangle, Binfo.inclangl
- ' with all answers in degrees (Ainfo.inclangl = Binfo.inclangl, of course)
- '
- TYPE vecsymb
- oper AS STRING * 1
- END TYPE
- ' vector operator - can select addition, subtraction, dot product, cross product
- DECLARE SUB vecoper (oper AS vecsymb, C AS Vectortype, b AS Vectortype, a AS Vectortype)
- ' syntax: call vecoper(vecsymb, C, B, A)
- ' result of opweration in C
- ' legal opoerations are:
- ' C = A + B - vector addition
- ' C = A - B - vector subtraction
- ' C = A . B - vector dot product
- ' C = A * B - vector cross product
- ' e.g.
- ' DIM vecsign AS vecsymb
- ' vecsign.oper = "*"
- ' A.xcoord = 1
- ' .
- ' .
- ' .
- ' CALL vecoper(vecsign, C, B, A)
- ' results in C.xcoord = A * B
- ' all other operations result in Cx, Cy, and Cz being set
- ' an illegal operation results in the vector C = (0,0,0)
- '
- TYPE tritype
- tristr AS STRING * 3
- ' legal entries for tristr are:
- ' "ASA", "SAS", "AAS", "SSA" ,"SSS"
- END TYPE
- TYPE triangleinfo
- solution AS INTEGER ' -1 means no solution, 0 means solution OK
- side1 AS INTEGER ' length of side1
- side2 AS INTEGER ' length of side2
- side3 AS INTEGER ' length of side3
- oppangl1 AS INTEGER ' angle opposite side1 - in degrees
- oppangl2 AS INTEGER ' angle opposite side2 - in degrees
- oppangl3 AS INTEGER ' angle opposite side3 - in degrees
- END TYPE
- DECLARE FUNCTION TRIA (x AS ANY) ' called by triangle
- DECLARE FUNCTION TRIB (x AS ANY) ' called by triangle
- ' get all info on triangle if you know minimal info, listed in tritype
- DECLARE SUB triangle (descriptor AS tritype, val1%, val2%, val3%, trinfo AS triangleinfo)
- ' syntax: CALL triangle(tri, val1%, val2%, val3%, trinfo)
- ' example
- ' DIM tri AS tritype
- ' DIM trinfo AS triangleinfo
- ' tri.tristr = "SSS"
- ' CALL triangle(tri, 10, 10, 10, trinfo)
- ' this would return the info for this triangle in trinfo
- '
- DECLARE SUB cart2pol (x, y, r, theta) ' convert from cartesian coordinates
- ' to polar coordinates
- ' syntax CALL cart2pol(x, y, r, theta, phi)
- ' you just have to supply the cartesian coordinates
- ' x, y. This routine returns:
- ' r - distance from origin of coordinate system
- ' theta - angle in degrees
- DECLARE SUB pol2cart (x, y, r, theta) ' convert from polar
- ' to cartesian coordinates
- ' syntax CALL pol2cart(x, y, r, theta)
- ' you just supply the polar coordinates.
- ' this routine returns the cartesian coordinates
- '
- DECLARE FUNCTION deg2rad (DEGS, MINS, SECS) ' convert from degrees to radians
- ' syntax rads = deg2rad(degs, mins, secs)
- ' degs are degrees of arc
- ' mins are minutes of arc
- ' secs are seconds of arc
- ' these are all single precision, floating point numbers
- ' so degrees.fraction_of_degrees is OK
- '
- DECLARE SUB rad2deg (rad, DEGS, MINS, SECS)
- ' syntax CALL rad2deg(rad, degs, mins, secs)
- ' you supply the radians (rad) and the values
- ' degs (degrees), mins (minutes), and secs (seconds) are returned
- '
- DECLARE FUNCTION lininterpy (x1, y1, x2, y2, x) ' linear interpolation
- ' of y - you supply two points (x1,y1) and (x2,y2)
- ' and an x value - the function returns the corresponding
- ' y value
- ' syntax y = lininterpy(x1, y1, x2, y2, x)
- '
- DECLARE FUNCTION lininterpx (x1, y1, x2, y2, y) ' linear interpolation
- ' of x - you supply two points (x1,y1) and (x2,y2)
- ' and a y value - the function returns the corresponding
- ' x value
- ' syntax x = lininterpx(x1, y1, x2, y2, y)
- '
- TYPE complex ' complex number
- real AS DOUBLE
- compart AS DOUBLE
- END TYPE
- '
- DECLARE SUB complexadd (C AS complex, b AS complex, a AS complex)
- ' add two complex numbers
- ' syntax DIM C AS complex, B AS complex, A AS complex
- ' CALL complexadd(C, B, A)
- ' results in C = A + B
- '
- DECLARE SUB complexsub (C AS complex, b AS complex, a AS complex)
- ' subtract two complex numbers
- ' syntax DIM C AS complex, B AS complex, A AS complex
- ' CALL complexsub(C, B, A)
- ' results in C = A - B
- '
- DECLARE SUB complexmul (C AS complex, b AS complex, a AS complex)
- ' multiply two complex numbers
- ' syntax DIM C AS complex, B AS complex, A AS complex
- ' CALL complexmul(C, B, A)
- ' results in C = A * B
- '
- DECLARE SUB complexdiv (C AS complex, b AS complex, a AS complex)
- ' divide two complex numbers
- ' syntax DIM C AS complex, B AS complex, A AS complex
- ' CALL complexdiv(C, B, A)
- ' results in C = A / B
- '
- DECLARE SUB complexroot (root%, b AS complex, a AS complex)
- ' get root of complex number
- ' syntax DIM B AS complex, A AS complex
- ' CALL complexroot(root%, B, A)
- ' results in B.real = magnitude(A ^ root%)
- ' B.compart = B.real
- ' the actual root is B.real*(cos(theta/root%) + i*sin(theta/root%))
- '
- DECLARE SUB quadratic (a, b, C, x1 AS complex, x2 AS complex) ' get roots of quadratic equation
- ' syntax DIM x1 AS complex: DIM x2 as complex
- ' CALL quadratic(a, b, c, x1, x2)
- ' where you supply the coefficients a, b and c
- ' for the equation a * x^2 + b * x + c = 0
- ' real parts of roots are in real part of number
- ' e.g., real part of x1 = x1.real
- ' e.g., complex part of x1 = x1.compart
- '
- DECLARE SUB newton (degree%, root!, incs!, coord$)
- ' real roots of equation of degree degree%
- ' coefficients can range in value from -coeff! to +coeff!
- ' the newton algorithm will search in increments of incs!
- ' from -root! to +root! searching for roots to the equation
- ' syntax CALL newton(degree%, root, incs, coord$)
- ' where coord$ is of the same form as in polyarea
- ' e.g., to solve for the roots of 2 * x ^ 3 + 4 * x ^ 2 + x - 3 = 0
- ' coord$ = "2,3:4,2:1,1:-3,0:"
- ' degree% = 3 3rd degree equation
- ' root = 10 try roots from -10 to 10
- ' incs = 0.1
- ' CALL newton(degree%, root, incs, coord$)
- ' i.e., coord$ is of the form "coefficient, power:"
- ' NOTE: if the coefficient is zero you dont have to include it
- ' UNLESS THE POWER IS 0, THEN YOU MUST INCLUDE 0,0: AT THE END OF coord$
- ' otherwise you will get a run time error
- ' the results (the roots of the equation) are stored in coord$ and can
- ' be parsed out after the call is made (roots delimited by ",")
- '
-
-